<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户" prop="deptId">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择商户">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
              ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="" prop="catId" v-if="false">
        <el-input
          v-model="queryParams.catId"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告位置" prop="pid">
        <el-select v-model="queryParams.pid" placeholder="请选择广告位置" clearable>
          <el-option
            v-for="dict in bl_ad_position"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="媒介类型" prop="mediaType">
        <el-select v-model="queryParams.mediaType" placeholder="请选择媒介类型" clearable>
          <el-option
            v-for="dict in bl_ad_media"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="广告类型" prop="adTypeId">
        <el-select v-model="queryParams.adTypeId" placeholder="请选择广告类型" clearable>
          <el-option
            v-for="dict in bl_ad_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="广告名称" prop="adName">
        <el-input
          v-model="queryParams.adName"
          placeholder="请输入广告名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接地址" prop="adLink" v-if="false">
        <el-input
          v-model="queryParams.adLink"
          placeholder="请输入链接地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投放时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择投放时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="添加人" prop="linkMan" >
        <el-input
          v-model="queryParams.linkMan"
          placeholder="请输入添加人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="添加人邮箱" prop="linkEmail" v-if="false">
        <el-input
          v-model="queryParams.linkEmail"
          placeholder="请输入添加人邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="添加人联系电话" prop="linkPhone" v-if="false">
        <el-input
          v-model="queryParams.linkPhone"
          placeholder="请输入添加人联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点击量" prop="clickCount" v-if="false">
        <el-input
          v-model="queryParams.clickCount"
          placeholder="请输入点击量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择是否显示" clearable>
          <el-option
            v-for="dict in bl_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="orderby" v-if="false">
        <el-input
          v-model="queryParams.orderby"
          placeholder="请输入排序"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否开启新窗口" prop="target" v-if="false">
        <el-select v-model="queryParams.target" placeholder="请选择是否开启新窗口" clearable>
          <el-option
            v-for="dict in bl_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="背景颜色" prop="bgcolor" v-if="false">
        <el-input
          v-model="queryParams.bgcolor"
          placeholder="请输入背景颜色"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['bl:ad:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bl:ad:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bl:ad:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['bl:ad:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告id" align="center" prop="adId" v-if="false"/>
      <el-table-column label="机构ID" align="center" prop="deptId" v-if="false"/>
      <el-table-column label="" align="center" prop="catId" v-if="false"/>
      <el-table-column label="广告位置" align="center" prop="pid">
        <template #default="scope">
          <dict-tag :options="bl_ad_position" :value="scope.row.pid"/>
        </template>
      </el-table-column>
      <el-table-column label="媒介类型" align="center" prop="mediaType">
        <template #default="scope">
          <dict-tag :options="bl_ad_media" :value="scope.row.mediaType"/>
        </template>
      </el-table-column>
      <el-table-column label="广告类型" align="center" prop="adTypeId">
        <template #default="scope">
          <dict-tag :options="bl_ad_type" :value="scope.row.adTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="广告名称" align="center" prop="adName" />
      <el-table-column label="链接地址" align="center" prop="adLink" />
      <el-table-column label="广告图片" align="center" prop="adImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.adImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="投放时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" prop="linkMan" />
      <el-table-column label="添加人邮箱" align="center" prop="linkEmail" width="180"/>
      <el-table-column label="添加人联系电话" align="center" prop="linkPhone" width="180"/>
      <el-table-column label="点击量" align="center" prop="clickCount" />
      <el-table-column label="是否显示" align="center" prop="enabled">
        <template #default="scope">
          <dict-tag :options="bl_yes_no" :value="scope.row.enabled"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderby" />
      <el-table-column label="是否开启新窗口" align="center" prop="target" width="180">
        <template #default="scope">
          <dict-tag :options="bl_yes_no" :value="scope.row.target"/>
        </template>
      </el-table-column>
      <el-table-column label="背景颜色" align="center" prop="bgcolor" />
      <el-table-column label="广告描述" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bl:ad:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bl:ad:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户广告对话框 -->
    <el-dialog :title="title" v-model="open" width="650px" append-to-body>
      <el-form ref="adRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户" prop="deptId">
            <el-select v-model="form.deptId" filterable placeholder="请选择">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
              ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="" prop="catId" v-if="false">
          <el-input v-model="form.catId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="广告位置" prop="pid">
          <el-select v-model="form.pid" placeholder="请选择广告位置">
            <el-option
              v-for="dict in bl_ad_position"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告类型" prop="mediaType">
          <el-select v-model="form.mediaType" placeholder="请选择广告类型">
            <el-option
              v-for="dict in bl_ad_media"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告类型" prop="adTypeId">
          <el-select v-model="form.adTypeId" placeholder="请选择广告类型">
            <el-option
              v-for="dict in bl_ad_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告名称" prop="adName">
          <el-input v-model="form.adName" placeholder="请输入广告名称" />
        </el-form-item>
        <el-form-item label="链接地址" prop="adLink">
          <el-input v-model="form.adLink" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="广告图片" prop="adImg">
          <image-upload v-model="form.adImg"/>
        </el-form-item>
        <el-form-item label="投放时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择投放时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="添加人" prop="linkMan">
          <el-input v-model="form.linkMan" placeholder="请输入添加人" />
        </el-form-item>
        <el-form-item label="添加人邮箱" prop="linkEmail">
          <el-input v-model="form.linkEmail" placeholder="请输入添加人邮箱" />
        </el-form-item>
        <el-form-item label="添加人联系电话" prop="linkPhone">
          <el-input v-model="form.linkPhone" placeholder="请输入添加人联系电话" />
        </el-form-item>
        <el-form-item label="点击量" prop="clickCount" v-if="false">
          <el-input v-model="form.clickCount" placeholder="请输入点击量" />
        </el-form-item>
        <el-form-item label="是否显示" prop="enabled">
          <el-select v-model="form.enabled" placeholder="请选择是否显示">
            <el-option
              v-for="dict in bl_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="orderby">
          <el-input v-model="form.orderby" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="是否开启新窗口" prop="target">
          <el-select v-model="form.target" placeholder="请选择是否开启新窗口">
            <el-option
              v-for="dict in bl_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="背景颜色" prop="bgcolor">
          <el-input v-model="form.bgcolor" placeholder="请输入背景颜色" />
        </el-form-item>
        <!-- <el-form-item label="广告描述">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item> -->
        <el-form-item label="广告描述" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Ad">
import { listAd, getAd, delAd, addAd, updateAd } from "@/api/bl/ad";
import { listDeptAll } from "@/api/bl/dept";

const { proxy } = getCurrentInstance();
const { bl_ad_media, bl_ad_type, bl_ad_position, bl_yes_no } = proxy.useDict('bl_ad_media', 'bl_ad_type', 'bl_ad_position', 'bl_yes_no');

const adList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: null,
    catId: null,
    pid: null,
    mediaType: null,
    adTypeId: null,
    adName: null,
    adLink: null,
    adImg: null,
    startTime: null,
    endTime: null,
    linkMan: null,
    linkEmail: null,
    linkPhone: null,
    clickCount: null,
    enabled: null,
    orderby: null,
    target: null,
    bgcolor: null,
    content: null,
  },
  rules: {
    deptId: [
      { required: true, message: "机构ID不能为空", trigger: "blur" }
    ],
    pid: [
      { required: true, message: "广告位置ID不能为空", trigger: "change" }
    ],
    mediaType: [
      { required: true, message: "广告类型不能为空", trigger: "change" }
    ],
    adTypeId: [
      { required: true, message: "广告类型不能为空", trigger: "change" }
    ],
    adName: [
      { required: true, message: "广告名称不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "投放时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    enabled: [
      { required: true, message: "是否显示不能为空", trigger: "change" }
    ],
    target: [
      { required: true, message: "是否开启浏览器新窗口不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商户广告列表 */
function getList() {
  loading.value = true;
  listAd(queryParams.value).then(response => {
    adList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    adId: null,
    deptId: null,
    catId: null,
    pid: null,
    mediaType: null,
    adTypeId: null,
    adName: null,
    adLink: null,
    adImg: null,
    startTime: null,
    endTime: null,
    linkMan: null,
    linkEmail: null,
    linkPhone: null,
    clickCount: null,
    enabled: null,
    orderby: null,
    target: null,
    bgcolor: null,
    content: null,
    articleId: null,
    code: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("adRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.adId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商户广告";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _adId = row.adId || ids.value
  getAd(_adId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商户广告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["adRef"].validate(valid => {
    if (valid) {
      if (form.value.adId != null) {
        updateAd(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAd(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _adIds = row.adId || ids.value;
  proxy.$modal.confirm('是否确认删除商户广告编号为"' + _adIds + '"的数据项？').then(function() {
    return delAd(_adIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bl/ad/export', {
    ...queryParams.value
  }, `ad_${new Date().getTime()}.xlsx`)
}

/** 查询商户信息列表 */
function getDeptAllList() {
listDeptAll(queryParams.value).then(response => {
  let deptListTmp = response.data;
  deptOptions.value = deptListTmp.filter(item => item.deptId !== 100);
  });
}
getDeptAllList();
getList();
</script>
